package com.hcb.micro.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.hcb.micro.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.service.IBuyBackService;
import com.hcb.micro.service.IMallService;
import com.hcb.micro.service.IOrderProductService;
import com.hcb.micro.service.IProductService;
import com.hcb.micro.service.IQqbsAccountService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("buyBack")
public class BuyBackController extends BaseControllers{
	
	@Autowired
	IQqbsAccountService qqbsAccountService;	
	
	@Autowired
	IBuyBackService buyBackService;	

	@Autowired
	IMallService mallService;
	
	@Autowired
	IOrderProductService orderProductService;
	
	@Autowired
	IProductService productService;
	
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		if(bodyInfo.get("accountId") != null && !"".equals(bodyInfo.get("accountId"))) {
			para.put("accountId", bodyInfo.getString("accountId"));
		}
		if(bodyInfo.get("status") != null && !"".equals(bodyInfo.get("status"))) {
			para.put("status", bodyInfo.getString("status"));
		}
		
		List<BuyBack> buyBackListAll=buyBackService.selectByPara(para);

		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}

		List<BuyBack> buyBackList=buyBackService.selectByPara(para);
		
		if(buyBackList.size() <= 0){
			json.put("result", "1");
			json.put("description", "没有回购信息！");
			return buildReqJsonObject(json);
		}
		List<Map<String,String>> list = new ArrayList<>();
		for(BuyBack bbk:buyBackList){
			Map<String,String> map = new HashMap<>();
			QqbsAccount account = qqbsAccountService.selectByAccountId(bbk.getAccountId());
			OrderProduct oprt=new OrderProduct();
			oprt.setOrderId(bbk.getOrderId());
			oprt.setProductId(bbk.getProductId());			
			OrderProduct opt=orderProductService.selectByOrderIdAndProductId(oprt);
			Product prod=productService.selectByPrimaryKey(opt.getProductId());
			
			//客户信息
			map.put("id", bbk.getId()+"");
			map.put("accountId", bbk.getAccountId()+"");
			map.put("accountNickName", account.getNickname()==null?"":account.getNickname());
			
			//商品信息
			map.put("productName", prod.getName()+"");
			map.put("salePrice", prod.getSalesPrice()+"");
			map.put("productCertificateNumber", opt.getCertificateNumber());
			if(bbk.getType()==1){
				map.put("buyBackType", "等值回购");
			}else{
				map.put("buyBackType", "扣费回购");
			}
			
			//客户身份信息
			map.put("personID", bbk.getPersonId());
			map.put("realName", bbk.getRealName());
			map.put("phone", bbk.getPhone());
			
			//快递信息
			map.put("logisticsNumber", bbk.getLogisticsNumber());
			map.put("logisticsName", bbk.getLogisticsName());
			map.put("realAddress", bbk.getRealAddress());
			
			map.put("wechatBank", bbk.getWechatBank());
			map.put("remark", bbk.getRemark());
			map.put("certificateNumber", bbk.getCertificateNumber());
			map.put("price", bbk.getPrice()+"");

			//回购进度状态
			String statusString = "";
			String buttonString="";
			switch(bbk.getStatus()){
			case 1:
				statusString = "申请中";
				buttonString = "同意";
				break;
			case 2:
				statusString = "待退货";
				buttonString = "确认收货";
				break;
			case 3:
				statusString = "待退款";
				buttonString = "打款";
				break;
			case 4:
				statusString = "退款成功";
				buttonString = "回购完成";
				break;
			case 5:
				statusString = "退款关闭";
				break;
			case 6:
				statusString = "退款取消";
				break;
			}
			map.put("statusString", statusString);
			map.put("status", bbk.getStatus()+"");
			map.put("buttonString", buttonString+"");
			//商品类型状态，是否可回购
			String prostatus = "";
			switch(prod.getType()){
			case 0:
				prostatus = "不可回购";
				break;
			case 1:
				prostatus = "可回购";
				break;			
			}
			map.put("prostatus", prostatus);
			
			list.add(map);
		}
		
		session.setAttribute("ORDERLIST", list);
		
    	json.put("result", "0");
		json.put("description", "获取订单列表成功");
		json.put("list", list);
		json.put("count", buyBackListAll.size());
		return buildReqJsonObject(json);
	}
	
	
	@RequestMapping(value = "toupdate", method = RequestMethod.POST)
	@ResponseBody
	public String toupdate() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		if (bodyInfo.get("buyBackId") == null ||"".equals(bodyInfo.get("buyBackId"))) {
			json.put("result", "1");
			json.put("description", "参数错误！");
			return buildReqJsonObject(json);
		}
		
		int buyBackId=bodyInfo.getInt("buyBackId");
		
		BuyBack buyBack=buyBackService.selectByPrimaryKey(buyBackId);
		buyBack.setStatus((byte) (buyBack.getStatus()+1));
		if(buyBack.getStatus()<=4){
			buyBackService.updateByPrimaryKeySelective(buyBack);
		}
		json.put("result", "0");
		json.put("description", "保存成功！");

		return buildReqJsonObject(json);
	}
	
	
	
}
